package lastfm;

import de.umass.lastfm.Artist;
import de.umass.lastfm.PaginatedResult;
import de.umass.lastfm.Period;
import de.umass.lastfm.Track;
import de.umass.lastfm.User;
import java.util.Collection;
import java.util.Iterator;


public class LastFm {
    private static final String API_KEY = "b152ae3f52ade79879dbbfd8e1d41c00";

    private Track track;

    private Collection<Track> trackCollection;
    private Iterator<Track> trackIterator;

    private Artist artist;

    private Collection<Artist> artistCollection;
    private Iterator<Artist> artistIterator;

    public String getNowPlaying(String username) {
        PaginatedResult<Track> pag = User.getRecentTracks(username, 1, API_KEY, 1);
        trackCollection = pag.getPageResults();

        trackIterator = trackCollection.iterator();

        if(trackIterator.hasNext()) {
            track = trackIterator.next();
        }
        else {
            return null;
        }
        String playedWhen = null;
        if(track.getPlayedWhen() != null) {
            playedWhen = track.getPlayedWhen().toString();
        }

        String nowPlaying;
        String album;

        if(track.getAlbum().equals("")) {
            album = "";
        }
        else {
            album = "(" + track.getAlbum() + ")";
        }
        
        if(playedWhen == null) {
            nowPlaying = track.getArtist() + " - " + track.getName() + " " + album + " <Listening now>";
        }
        else {
            nowPlaying = track.getArtist() + " - " + track.getName() + " " + album + " <" + playedWhen + ">";
        }

        return nowPlaying;
    }
    public String getMostPlayedTrack(String username) {
        String period = "";
        try {
            trackCollection = User.getTopTracks(username, Period.OVERALL, API_KEY);
        } catch (Exception e) {
            period = "last.fm e 1 jävla frasröra så du får bara för de s1aste 12 månaderna :<";
            try {
                trackCollection = User.getTopTracks(username, Period.TWELVE_MONTHS, API_KEY);

            } catch (Exception e2) {
                period = "last.fm e 1 jävla frasröra så du får bara för de s1aste 3 månaderna :<";
                try {
                    trackCollection = User.getTopTracks(username, Period.THREE_MONTHS, API_KEY);
                } catch(Exception e3) {
                    return "last.fm gillar inte " + username + " :<";
                }
            }
        }
        trackIterator = trackCollection.iterator();

        if(trackIterator.hasNext()) {
            track = trackIterator.next();
        }
        else {
            return null;
        }

        track.getArtist();

        return track.getArtist() + " - " + track.getName() + " (" + track.getPlaycount() + " plays) " + period;
    }
    public String getMostPlayedArtist(String username) {

        try {
            artistCollection = User.getTopArtists(username, Period.OVERALL, API_KEY);
        } catch (Exception e) {
            try {
                artistCollection = User.getTopArtists(username, Period.TWELVE_MONTHS, API_KEY);

            } catch (Exception e2) {
                try {
                    artistCollection = User.getTopArtists(username, Period.THREE_MONTHS, API_KEY);
                } catch(Exception e3) {
                    return "last.fm gillar inte " + username + " :<";
                }
            }
        }

        artistIterator = artistCollection.iterator();
        
        if(artistIterator.hasNext()) {
            artist = artistIterator.next();
        }
        else {
            return null;
        }

        return artist.getName() + " (" + artist.getPlaycount() + " plays)";
    }
}